﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Net5ApiAndEfMySql.DtoModel;
using Net5ApiAndEfMySql.Repository;
using Net5CommonLib.SystemLib.TypeTools;
using VueAndNetCoreEfApi.DtoModel.SystemAdmin;
using VueAndNetCoreEfApi.IRepository.SystemAdmin;
using VueAndNetCoreEfApi.PoModel.SystemAdmin;

namespace VueAndNetCoreEfApi.Repository.SystemAdmin
{
    public class OwnerRepository:BaseRepository,IOwnerRepository
    {

        public PagerResult<OwnerPo> GetPageList(SearchModel m)
        {
            IQueryable<OwnerPo> iq = SoaSystemCenterDbContext.OwnerPo.AsQueryable();
            if (m.SearchItem != null && m.SearchItem.Count > 0)
            {
                var tempDi = m.SearchItem;


                string owName = "ow_name";
                if (tempDi.Keys.Contains(owName) && !string.IsNullOrEmpty(tempDi[owName]))
                {
                    iq = iq.Where(c => c.ow_name.Contains(tempDi[owName]));
                }


                string owtel = "ow_tel";
                if (tempDi.Keys.Contains(owtel) && !string.IsNullOrEmpty(tempDi[owtel]))
                {
                    iq = iq.Where(c => c.ow_tel.Contains(tempDi[owtel]));
                }


                string owvalidity = "ow_validity";
                if (tempDi.Keys.Contains(owvalidity) && !string.IsNullOrEmpty(tempDi[owvalidity]))
                {
                    iq = iq.Where(c => c.ow_validity < DateTime.Parse(tempDi[owvalidity]));
                }
            }


            return Pager(iq, m);



        }

        public bool OwnerAdd(OwnerDto Owner)
        {
            if (Owner == null) return false;
            //起一个事务
            using (var tran = SoaSystemCenterDbContext.Database.BeginTransaction())
            {
                try
                {
                    Owner.ow_createtime=DateTime.Now;
                    //添加用户
                    SoaSystemCenterDbContext.Add(Owner);
                    //保存用户的添加
                    SoaSystemCenterDbContext.SaveChanges();
                    //提交事务
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();

                }

            }
            return true;

        }


        //删除
        public bool DelOwnerById(int ow_id)
        {
            if (ow_id == 0) return false;
            using (var tran = SoaSystemCenterDbContext.Database.BeginTransaction())
            {
                try
                {
                    var ownerPo = SoaSystemCenterDbContext.OwnerPo;
                    var ownerPoOne = SoaSystemCenterDbContext.OwnerPo.FirstOrDefault(c => c.ow_id == ow_id);
                    if (ownerPo == null)
                    {
                        return false;
                    }

                    SoaSystemCenterDbContext.OwnerPo.RemoveRange(ownerPoOne);
                    SoaSystemCenterDbContext.SaveChanges();
                    tran.Commit();
                }
                catch (Exception ex)
                {
                    tran.Rollback();
                }
            }
            return true;
        }

        //返填
        public OwnerPo GetOwnerById(int ow_id)
        {
            if (ow_id == 0) return null;
            var ownerPoOne = SoaSystemCenterDbContext.OwnerPo.FirstOrDefault(c => c.ow_id == ow_id);

            return ownerPoOne;
        }

        public bool OwnerUpdate(OwnerPo owner)
        {
            if (owner == null) return false;

            SoaSystemCenterDbContext.Update(owner);
            SoaSystemCenterDbContext.SaveChanges();
            return true;
        }

    }
}
